package com.weirong.smartvault.backservice.controller;

import com.weirong.smartvault.backservice.controller.fragment_entity.PagesIndex;
import com.weirong.smartvault.backservice.service.CommandService;
import com.weirong.smartvault.backservice.service.ForeignCurrencyRequestService;
import com.weirong.smartvault.common.tools.constant.ThymleafVarName;
import com.weirong.smartvault.backservice.util.table.CommandTable;
import com.weirong.smartvault.backservice.util.table.Table;
import com.weirong.smartvault.backservice.util.common.ListTool;
import com.weirong.smartvault.entity.ForeignCurrencyRequest;
import com.weirong.smartvault.backservice.entity_model.CommandModel;
import com.weirong.smartvault.backservice.util.common.DateUtil;
import com.weirong.smartvault.dao.CommandRepository;
import com.weirong.smartvault.backservice.entity_model.ForeignCurrencyRequestModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.*;

/**
 * 于2017/11/19由 KFEB4 创建 .
 */
@Controller
public class Command {
    @Autowired
    CommandRepository commandRepository;

    @Autowired
    CommandService commandService;


    @Autowired
    ForeignCurrencyRequestService foreignCurrencyRequestService;

    @RequestMapping(value = "/command/command_requests", method = RequestMethod.GET)
    public String commandRequests(ModelMap model, @RequestParam(value = "page", defaultValue = "0") Integer page, @RequestParam(value = "size", defaultValue = "15") Integer size) {
        Sort sort = new Sort(Sort.Direction.ASC, "id");
        Pageable pageable = new PageRequest(page, size, sort);
        Page<com.weirong.smartvault.entity.Command> departmentPage = commandRepository.findAll(pageable);
        model.addAttribute("commands", departmentPage);

        model.addAttribute("PagesIndex", new PagesIndex(page, departmentPage.getTotalPages()));


        return "command/command_requests";
    }


    @RequestMapping(value = "/command/command", method = RequestMethod.GET)
    public String commands(ModelMap model, @RequestParam(value = "page", defaultValue = "0") Integer page,
                           @RequestParam(value = "size", defaultValue = "10") Integer size,@RequestParam(value = "date", defaultValue = "") String date) throws ParseException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException, NoSuchFieldException {
        Date start = DateUtil.getTodayOrParse(date);

        Page<ForeignCurrencyRequest> requests = foreignCurrencyRequestService.findAllByCreateTimeBetweenWithPageing(start,DateUtil.getDateNext(start),page,size);
        List<ForeignCurrencyRequestModel> requestModels = ListTool.cast(requests, ForeignCurrencyRequestModel.class);
        Table<CommandModel> table1 = CommandTable.buildRequestTable(requestModels);

        model.addAttribute(ThymleafVarName.Var.commandCollections,table1);

        //生成各个指令的详细内容
        List<Table> commandTables = new ArrayList<>();
        for(ForeignCurrencyRequestModel foreignCurrencyRequestModel:requestModels){
            commandTables.add(CommandTable.buildTable(foreignCurrencyRequestModel));
        }

        model.addAttribute(ThymleafVarName.Var.commandTables,commandTables);

        model.addAttribute(ThymleafVarName.pages, new PagesIndex(page,requests.getTotalPages(),"date",date));
        return "command/command";
    }




}
